/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * File Name          : freertos.c
 * Description        : Code for freertos applications
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "message_buffer.h"
#include <stdio.h>
#include <string.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define MSG_BUFFER_LEN	50
#define MSG_MAX_LEN	    20
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
MessageBufferHandle_t msgBuffer;
/* USER CODE END Variables */
/* Definitions for Task_Show */
osThreadId_t Task_ShowHandle;
const osThreadAttr_t Task_Show_attributes = { .name = "Task_Show", .stack_size =
		256 * 4, .priority = (osPriority_t) osPriorityNormal, };

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void AppTask_Show(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
 * @brief  FreeRTOS initialization
 * @param  None
 * @retval None
 */
void MX_FREERTOS_Init(void) {
	/* USER CODE BEGIN Init */

	/* USER CODE END Init */

	/* USER CODE BEGIN RTOS_MUTEX */
	/* add mutexes, ... */
	/* USER CODE END RTOS_MUTEX */

	/* USER CODE BEGIN RTOS_SEMAPHORES */
	/* add semaphores, ... */
	/* USER CODE END RTOS_SEMAPHORES */

	/* USER CODE BEGIN RTOS_TIMERS */
	/* start timers, add new ones, ... */
	/* USER CODE END RTOS_TIMERS */

	/* USER CODE BEGIN RTOS_QUEUES */
	/* add queues, ... */
	/* USER CODE END RTOS_QUEUES */

	/* Create the thread(s) */
	/* creation of Task_Show */
	Task_ShowHandle = osThreadNew(AppTask_Show, NULL, &Task_Show_attributes);

	/* USER CODE BEGIN RTOS_THREADS */
	/* add threads, ... */
	msgBuffer = xMessageBufferCreate(MSG_BUFFER_LEN);
	/* USER CODE END RTOS_THREADS */

	/* USER CODE BEGIN RTOS_EVENTS */
	/* add events, ... */
	/* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_AppTask_Show */
/**
 * @brief  Function implementing the Task_Show thread.
 * @param  argument: Not used
 * @retval None
 */
/* USER CODE END Header_AppTask_Show */
void AppTask_Show(void *argument) {
	/* USER CODE BEGIN AppTask_Show */
	uint8_t receive_data[MSG_MAX_LEN];
	/* Infinite loop */
	for (;;) {
		uint16_t realCnt = xMessageBufferReceive(msgBuffer, receive_data,
				MSG_MAX_LEN, portMAX_DELAY);

		uint8_t temp_str[30];
		sprintf("Read message bytes = %d", realCnt);
		lcd_show_str(10, 10 + 2 * 30, 24, temp_str, RED);

		sprintf(temp_str, "%s       ", receive_data);
		lcd_show_str(10, 10 + 3 * 30, 24, temp_str, RED);
	}
	/* USER CODE END AppTask_Show */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc) {
	RTC_TimeTypeDef sTime;
	RTC_DateTypeDef sData;
	if (HAL_RTC_GetTime(hrtc, &sTime, RTC_FORMAT_BIN) == HAL_OK) {
		if (HAL_RTC_GetDate(hrtc, &sData, RTC_FORMAT_BIN) == HAL_OK) {
			uint8_t temp_str[20];
			sprintf(temp_str, "%d:%d:%d", sTime.Hours, sTime.Minutes,
					sTime.Seconds);
			uint8_t bytesCount = strlen(temp_str);

			if (msgBuffer != NULL) {
				BaseType_t pxHigherPriorityTaskWoken = pdFALSE;
				uint16_t realCnt = xMessageBufferSendFromISR(msgBuffer,
						temp_str, bytesCount + 1, &pxHigherPriorityTaskWoken);

				uint8_t temp_str_lcd[20];
				sprintf(temp_str_lcd, "Write bytes = %d", realCnt);
				lcd_show_str(10, 10 + 1 * 30, 24, temp_str_lcd, RED);
				portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
			}
		}
	}
}
/* USER CODE END Application */

